package com.seaboxdata.interfaceJob;

import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.Warning;
import com.seaboxdata.kafka.BaseStringKafkaProducer;
import com.seaboxdata.service.WarningService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Configuration
@Slf4j
public class AlarmMsgCollectJob {

    @Autowired
    private WarningService warningService;

    @Autowired
    private AlarmMsgSynchronizer alarmMsgSynchronizer;

    @Value("${whp.warningTopic}")
    private String topic;

    @Value("${interface.isTest}")
    private String isTest;

    @Autowired
    private BaseStringKafkaProducer baseStringKafkaProducer;

    @Scheduled(cron = "${crontab.warnning}")//每隔5分钟执行一次
    public void doCollect() {
        log.info("collect whpWarning begin");
        //如果是测试,直接退出
        if("true".equals(isTest)){
            return;
        }
        //定时获取告警数据,查看数据库是否有告警信息,没有新增
        List<Warning> list = alarmMsgSynchronizer.queryWarnningRecords();
        if (!CollectionUtils.isEmpty(list)) {
            for (Warning warning : list) {
                try {
                    baseStringKafkaProducer.sendMessageToKafka(JSONObject.toJSONString(warning), topic);
                } catch (Exception e) {
                    log.error("write whpWarning error");
                }
            }
        }
    }
}
